package com.bootx.util.lottery;

import com.bootx.entity.LotteryRecord;
import com.bootx.util.DateUtils;
import com.bootx.util.JsonUtils;
import com.bootx.util.WebUtils;
import com.bootx.util.lottery.pojo.DrawLotteryData;
import com.bootx.util.lottery.pojo.DrawLotteryHistoryData;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LotteryUtils {

    public static Map<String,LotteryRecord> getData(Long lotteryCode, Integer sn){
        String s = WebUtils.get("https://api.api68.com/CQShiCai/getBaseCQShiCai.do?issue="+sn+"&lotCode="+lotteryCode);
        DrawLotteryData drawLotteryData = JsonUtils.toObject(s, new TypeReference<DrawLotteryData>() {
        });
        DrawLotteryData.ResultDTO result = drawLotteryData.getResult();
        DrawLotteryData.ResultDTO.DataDTO data = result.getData();
        LotteryRecord current = new  LotteryRecord();
        current.setLotteryCode(data.getLotCode());
        current.setDrawTime(DateUtils.formatStringToDate(data.getPreDrawTime(),"yyyy-MM-dd HH:mm:ss"));
        current.setSn(data.getPreDrawIssue());
        current.setDrawCode(data.getPreDrawCode());
        LotteryRecord next = new LotteryRecord();
        next.setLotteryCode(data.getLotCode());
        next.setSn(data.getDrawIssue());
        next.setDrawTime(DateUtils.formatStringToDate(data.getDrawTime(),"yyyy-MM-dd HH:mm:ss"));
        return Map.of("current",current,"next",next);
    }

    public static List<LotteryRecord> history(Long lotteryCode){
        List<LotteryRecord> list = new ArrayList<LotteryRecord>();
        String s = WebUtils.get("https://api.api68.com/CQShiCai/getBaseCQShiCaiList.do?date=&lotCode="+lotteryCode);
        DrawLotteryHistoryData drawLotteryData = JsonUtils.toObject(s, new TypeReference<DrawLotteryHistoryData>() {
        });
        DrawLotteryHistoryData.ResultDTO result = drawLotteryData.getResult();
        List<DrawLotteryHistoryData.ResultDTO.DataDTO> data = result.getData();
        for (DrawLotteryHistoryData.ResultDTO.DataDTO datum : data) {
            LotteryRecord lotteryRecord = new  LotteryRecord();
            lotteryRecord.setLotteryCode(lotteryCode);
            lotteryRecord.setDrawTime(DateUtils.formatStringToDate(datum.getPreDrawTime(),"yyyy-MM-dd HH:mm:ss"));
            lotteryRecord.setSn(datum.getPreDrawIssue());
            lotteryRecord.setDrawCode(datum.getPreDrawCode());
            list.add(lotteryRecord);
        }

        return list;
    }


    public static void main(String[] args) {
        Map<String, LotteryRecord> data = getData(10036L, 0);
        System.out.println(data);
    }
}
